<%@page import="edu.ncsu.csc.itrust.dao.DAOFactory"%>
<%@page import="edu.ncsu.csc.itrust.action.PatientReferralAction"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientReferralDAO"%>
<%@page import="edu.ncsu.csc.itrust.beans.PersonnelBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.PatientBean"%>

<%@page import="edu.ncsu.csc.itrust.beans.PatientReferralBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.OfficeVisitBean"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientDAO"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.OfficeVisitDAO"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>

<%@include file="/global.jsp"%>

<%
	pageTitle = "iTrust - View Referrals";
%>

<%@include file="/header.jsp"%>

<%
String referral = request.getParameter("refID");
PatientReferralDAO patRefDAO = DAOFactory.getProductionInstance().getPatientReferralsDAO();
OfficeVisitDAO ovDAO = DAOFactory.getProductionInstance().getOfficeVisitDAO();
PersonnelDAO personnelDAO = DAOFactory.getProductionInstance().getPersonnelDAO();
PatientDAO patDAO = DAOFactory.getProductionInstance().getPatientDAO();

PatientReferralAction action = new PatientReferralAction(prodDAO, loggedInMID.longValue());
List<PatientReferralBean> patReferral = action.getReferralsSentFromMe();
List<PatientReferralBean> patReferral2 = action.getReferralsSentToMe();

//Log that the HCP has viewed their list of referrals.
loggingAction.logEvent(TransactionType.VIEW_LIST_PATIENT_REFERRALS, loggedInMID.longValue(), 0, "");

//Sorts messages
if(request.getParameter("sort") != null) {
	patReferral = action.sort(request.getParameter("sortby"),request.getParameter("sorthow"),"");
}
%>
<form method="post" action="viewReferrals.jsp">
<div align="center">
<table>
	<tr>
		<td><select name="sortby">
			<option value="name">Sort by</option>
			<option value="name">Patient Name</option>
			<option value="hcp">Receiving HCP Name</option>
			<option value="time">Time</option>
			<option value="priority">Priority</option>
		</select></td>
		<td><select name="sorthow">
			<option value="desc">Order by</option>
			<option value="asce">Ascending</option>
			<option value="desc">Descending</option>
		</select></td>
		<td><input type="submit" name="sort" value="Sort" /></td>
	</tr>
</table>
</div>
</form>
<br />

<input type="hidden" name="formIsFilled" value="true">
<div align="center">
<h1>My Referrals</h1>
<p>To sort your referrals, click the table header that you want to
sort by.</p>
<table class="sortable">
	<thead>
		<tr>
			<th align=center>Receiving HCP Name<input
				style="visibility: hidden;" type="button" id="Receiving HCP Name"></th>
			<th align=center>Patient Name<input style="visibility: hidden;"
				type="button" id="Patient Name"></th>
			<th align=center>Time Generated<input
				style="visibility: hidden;" type="button" id="Time Generated"></th>
			<th align=center>Priority<input style="visibility: hidden;"
				type="button" id="Priority"></th>
		</tr>
	</thead>
	<tbody>
		<%	for(PatientReferralBean p : patReferral) { 
	PersonnelBean sender = personnelDAO.getPersonnel(p.getSenderID());
	PersonnelBean receiver = personnelDAO.getPersonnel(p.getReceiverID());
	OfficeVisitBean ov = ovDAO.getOfficeVisit(p.getOfficeVisitID());
	PatientBean pat = patDAO.getPatient(p.getPatientID());
	%>
		<tr>
			<td><a
				href="viewReferrals.jsp?refID=<%= StringEscapeUtils.escapeHtml("" + (p.getId())) %>"><%= StringEscapeUtils.escapeHtml("" + (receiver.getFullName())) %></a></td>
			<td><%= StringEscapeUtils.escapeHtml("" + (pat.getFullName())) %></td>
			<td><%= StringEscapeUtils.escapeHtml("" + (p.getDateAdded().toString())) %></td>
			<td><%= StringEscapeUtils.escapeHtml("" + (p.getPriority())) %></td>
		</tr>
		<%} %>
	</tbody>
</table>
</div>
<br />

<input type="hidden" name="formIsFilled" value="true">
<div align="center">
<table class="fTable">
	<tr>
		<th colspan="4">Received Referrals(Ordered by Priority)</th>
	</tr>
	<tr>
		<td align=center>Sending HCP Name</td>
		<td align=center>Patient Name</td>
		<td align=center>Time Generated</td>
		<td align=center>Priority</td>
	</tr>
	<%	for(PatientReferralBean p : patReferral2) { 
	PersonnelBean sender = personnelDAO.getPersonnel(p.getSenderID());
	PersonnelBean receiver = personnelDAO.getPersonnel(p.getReceiverID());
	OfficeVisitBean ov = ovDAO.getOfficeVisit(p.getOfficeVisitID());
	PatientBean pat = patDAO.getPatient(p.getPatientID());
	%>
	<tr>
		<td><a
			href="viewReferrals.jsp?refID=<%= StringEscapeUtils.escapeHtml("" + (p.getId())) %>"><%= StringEscapeUtils.escapeHtml("" + (sender.getFullName())) %></a></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (pat.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (p.getDateAdded().toString())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (p.getPriority())) %></td>
	</tr>
	<%} %>
</table>
</div>
<br />

<%
if (referral != null && !referral.equals("")) {
	//List<HCPDiagnosisBean> hcps = action.getHCPByDiagnosis(icdcode); 
	//loggingAction.logEvent(TransactionType.EXPERIENCED_LHCP_FIND, loggedInMID.longValue(), 0, "Find LHCPs with experience with a diagnosis");
	PatientReferralBean bean = patRefDAO.getPatientReferral(Integer.parseInt(request.getParameter("refID")));
	PersonnelBean sender = personnelDAO.getPersonnel(bean.getSenderID());
	PersonnelBean receiver = personnelDAO.getPersonnel(bean.getReceiverID());
	OfficeVisitBean ov = ovDAO.getOfficeVisit(bean.getOfficeVisitID());
	PatientBean pat = patDAO.getPatient(bean.getPatientID());
	if(loggedInMID.longValue()==bean.getReceiverID()){
		bean.setBeenRead(1);
		patRefDAO.editReferral(bean);
	}
	loggingAction.logEvent(TransactionType.VIEW_PATIENT_REFERRAL, loggedInMID.longValue(), bean.getPatientID(), "");
%>
<div align="center">
<h2>Complete Referral Details</h2>
<table class="fTable">
	<tr>
		<th colspan="7">Complete Referral Details</th>
	</tr>
	<tr>
		<th>Patient Name</th>
		<th>Receiving HCP</th>
		<th>Receiving HCP Specialty</th>
		<th>Time Generated</th>
		<th>Priority</th>
		<th>Office Visit Date</th>
		<th>Notes</th>
	</tr>

	<tr>
		<td><%= StringEscapeUtils.escapeHtml("" + (pat.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (receiver.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (receiver.getSpecialty())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getDateAdded().toString())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getPriority())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (ov.getVisitDateStr())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getReferralDetails())) %></td>
	</tr>

</table>
</div>
<%
}%>


<%@include file="/footer.jsp"%>
